package com.fortune.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.Constants;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.dto.excution.DUserExcution;
import com.fortune.dto.in.dUser.DChangePasswordDto;
import com.fortune.dto.in.dUser.DLoginDto;
import com.fortune.dto.in.dUser.DResetPasswordDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DUserEnum;
import com.fortune.service.DUserService;
import com.fortune.vo.dUser.LoginDUserInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api(value = "duser", description = "经销商员工账号相关", tags = {"经销商员工账号相关"})
@RestController
@RequestMapping("dUser")
public class DUserController{
	
	@Autowired
	private DUserService dUserService;
	@Autowired
	private RedisClient redisClient;
	
	@ApiOperation(value = "经销商员工登录", nickname = "dUserLogin", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "login",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<LoginDUserInfo> login(HttpServletRequest request,@Validated @RequestBody DLoginDto dloginDto) {
			int check = dUserService.loginCheck(dloginDto);
			if (check==1) {
				return new DUserExcution<LoginDUserInfo>(null, DUserEnum.PASSWORDFALSE);
			}else if (check==2) {
				return new DUserExcution<LoginDUserInfo>(null, DUserEnum.PHONEFALSE);
			}else {
				DUserInfo dUserInfo = dUserService.dUserInfo(dloginDto);
				HttpSession session = request.getSession();
				session.setAttribute(Constants.SESSION_D_USER, dUserInfo);
				LoginDUserInfo loginDUserInfo = dUserService.loginDUserInfo(dloginDto);
				return new DUserExcution<LoginDUserInfo>(loginDUserInfo, DUserEnum.SUCCESS);
			}
	}
	
	@ApiOperation(value = "经销商员工退出登录", nickname = "dUserLogout", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "logout")
	public BaseAPIExcution<Void> logount(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute(Constants.SESSION_D_USER);
		return new DUserExcution<Void>(null,DUserEnum.SUCCESS);
	}
	
	@ApiOperation(value = "经销商员工重置密码", nickname = "dUserResetPassword", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "password/reset",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> resetPassword(HttpServletRequest request,@Validated @RequestBody DResetPasswordDto dResetPasswordDto) {
		redisClient.save("123456", 5, CacheKeys.SALES_MAN_LOGIN_TOKEN, "fda");
		String check = redisClient.get(CacheKeys.SALES_MAN_LOGIN_TOKEN, "fda");
		if (check==null || check.isEmpty()) {
			return new DUserExcution<Void>(null, DUserEnum.SESSIONCODEFALSE);
		}
		if (check.equals(dResetPasswordDto.getCode())) {
			HttpSession session = request.getSession();
			DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
			if (dUserInfo==null) {
				return new DUserExcution<Void>(null, DUserEnum.NOLOGIN);
			}
			int result = dUserService.resetPassword(dUserInfo, dResetPasswordDto);
			if (result==1) {
				return new DUserExcution<Void>(null, DUserEnum.SUCCESS);
			}
			return new DUserExcution<Void>(null, DUserEnum.FALSE);
		}
		return new DUserExcution<Void>(null, DUserEnum.CODEFALSE);
	}
	
	@ApiOperation(value = "经销商员工修改密码", nickname = "dUserChangePassword", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "password/change",
    	produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> changePassword(HttpServletRequest request,@Validated @RequestBody DChangePasswordDto dChangePasswordDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		if (dUserInfo==null) {
			return new DUserExcution<Void>(null, DUserEnum.NOLOGIN);
		}
		int result =dUserService.updatePassword(dUserInfo, dChangePasswordDto);
		if (result==0) {
			return new DUserExcution<Void>(null, DUserEnum.PASSWORDFALSE);
		}
		return new DUserExcution<Void>(null, DUserEnum.SUCCESS);
	}

}
